package cn.lhl01.sys.controller;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.security.Principal;


/**
 * Controller统一使用post请求
 */

@RestController
@RequestMapping("/user")
public class UserController {

	@PostMapping("/comm")
	@PreAuthorize("hasAuthority('/user/comm')")
	public String comm() {
		return "comm 111";
	}

	@PostMapping("/admin")
	@PreAuthorize("hasAuthority('/user/admin')")
	public String admin() {
		return "admin 121";
	}


	//获取 用户信息
	@PostMapping("/user1")
	public Object user1(Principal principal) {
		return principal;
	}

	@PostMapping("/user2")
	public Object user2(Authentication authentication) {
		return authentication;
	}

	@PostMapping("/user3")
	public Object user3(HttpServletRequest request) {
		return request.getUserPrincipal();
	}

	@PostMapping("/user4")
	public Object user4() {
		return SecurityContextHolder.getContext().getAuthentication();
	}


}
